package com.doubi.service;//package com.gaoguo.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.doubi.constants.SystemConstants;
import com.doubi.mapper.CityMapper;
import com.doubi.mapper.ProvinceMapper;
import com.doubi.model.sys.TCity;
import com.doubi.model.sys.TProvince;
import com.doubi.utils.DataUtil;
import com.doubi.vo.AppModel;
import com.doubi.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 功能说明：市区 服务层实现
 * @author MrDoubi
 * @date 2019-05-29 20:09:19
 */
@Service
public class CityService extends ServiceImpl<CityMapper, TCity> {

    @Autowired
    private CityMapper cityMapper;
    @Autowired
    private ProvinceMapper provinceMapper;


    /**
     * 功能说明：查询市区分页信息
     * @param page page
     * @param city bean
     * @return page
     */
    public PageVo<TCity> getPage(PageVo<TCity> page, TCity city) {
        QueryWrapper<TCity> wrapper = new QueryWrapper<>();
        if(DataUtil.isNotEmpty(city.getCityName())){
            wrapper.like("city_name",city.getCityName());
        }
        if(DataUtil.isNotEmpty(city.getCityCode())){
            wrapper.like("city_code",city.getCityCode());
        }
        if(DataUtil.isNotEmpty(city.getProvinceCode())){
            wrapper.like("province_code",city.getProvinceCode());
        }
        if(DataUtil.isNotEmpty(city.getStatus())){
            wrapper.eq("status",city.getStatus());
        }
        wrapper.orderByAsc("sort_num");
        cityMapper.selectPage(page,wrapper);
        return page;
    }

    /**
     * 功能说明：查询市区列表信息
     * @param city bean
     * @return list
     */
    public List<TCity> getList(TCity city) {
        QueryWrapper<TCity> wrapper = new QueryWrapper<>();
        if(DataUtil.isNotEmpty(city.getCityName())){
            wrapper.like("city_name",city.getCityName());
        }
        if(DataUtil.isNotEmpty(city.getCityCode())){
            wrapper.like("city_code",city.getCityCode());
        }
        if(DataUtil.isNotEmpty(city.getProvinceCode())){
            wrapper.like("province_code",city.getProvinceCode());
        }
        if(DataUtil.isNotEmpty(city.getStatus())){
            wrapper.eq("status",city.getStatus());
        }
        wrapper.orderByAsc("sort_num");
        return cityMapper.selectList(wrapper);
    }

    /**
     * 功能说明：市区 新增、修改
     * @param city bean
     * @return AppModel
     */
    public AppModel saveOrUpdateBean(TCity city) {
        TProvince province = provinceMapper.selectById(city.getProvinceOid());
        if(DataUtil.isEmpty(province)){
            return new AppModel(-1,"省份不存在！");
        }else {
            city.setProvinceCode(province.getProvinceCode());
        }
        //修改操作
        if (DataUtil.isNotEmpty(city.getCityOid())) {
            TCity bean = super.getById(city.getCityOid());
            if (DataUtil.isEmpty(bean)) {
                return new AppModel(-1, "参数错误！");
            }
            //进行 数据的拷贝
            bean.setStatus(city.getStatus());
            bean.setCityCode(city.getCityCode());
            bean.setCityName(city.getCityName());
            bean.setSortNum(city.getSortNum());
            cityMapper.updateAreaByCity(city.getCityOid(),city.getCityCode());
            cityMapper.updateById(bean);
        } else {
            //进行初始化操作

            cityMapper.insert(city);
        }
        return new AppModel(0,"操作成功！");
    }

    /**
     * 功能说明：市区 删除
     * @param city bean
     * @return AppModel
     */
    public AppModel delBean(TCity city) {
        city = cityMapper.selectById(city.getCityOid());
        if (DataUtil.isEmpty(city)) {
            return new AppModel(-1, "参数错误！");
        } else {
            cityMapper.delAreaByCity(city.getCityOid());
            cityMapper.deleteById(city.getCityOid());
            return new AppModel(0, "删除成功！");
        }
    }


    /**
     * 功能说明：市区 更新状态
     * @param city bean
     * @return AppModel
     */
    public AppModel updateStatus(TCity city) {
        city = cityMapper.selectById(city.getCityOid());
        if (DataUtil.isEmpty(city)) {
            return new AppModel(-1, "参数错误！");
        } else {
            if(city.getStatus().equals(SystemConstants.STATUS.STATUS_1)){
                city.setStatus(SystemConstants.STATUS.STATUS_0);
            }else {
                city.setStatus(SystemConstants.STATUS.STATUS_1);
            }
            cityMapper.updateById(city);
            return new AppModel(0, "修改成功！");
        }
    }
}
